package com.jetinno.analysis

import com.jetinno.utils.CanisterIds
import timber.log.Timber

/**
 * Created by zhangyuncai.
 * Date: 2022/1/20
 */
/**
 * 制作产品
 * FE
 * 05
 * 0003
 * 0018
 * 04005E01305A5F01
 * 0300E8036B5A5F01
 * AA008A02425A0000
 * 8854
 */
object Analysis05 {
    @JvmStatic
    fun analysis(cmd: String) {
        try {


            val byteList = mutableListOf<String>()
            for (index in 0 until cmd.length / 2) {
                val multi = index * 2
                val byte = cmd.substring(0 + multi, 2 + multi)
                byteList.add(byte)
            }

            Timber.e(start)
            //帧头
            val frameHeaderHex = byteList[0]
            Timber.e("帧头:%s", frameHeaderHex)
            Timber.e(line)
            //功能码
            val funCodeHex = byteList[1]
            Timber.e("功能码:%s", funCodeHex)
            Timber.e(line)
            //出料仓数
            val canisterNum = byteList[2] + byteList[3]
            Timber.e("出料仓数:%s→%d", canisterNum, canisterNum.toInt(16))
            Timber.e(line)
            //数据长度
            val dataLength = byteList[4] + byteList[5]
            Timber.e("数据长度:%s→%d", dataLength, dataLength.toInt(16))
            Timber.e(line)
//            val coffeeDesc = arrayOf("咖啡豆料仓", "出水间隔(s)", "高压加水量L(ml)", "高压加水量H(0.1ml)", "磨豆时间(s)", "去旧粉时间(s)", "预留", "预留")
//            val normalDesc = arrayOf("料仓编号", "出水间隔(s)", "加水量L(ml)", "加水量H(0.1ml)", "下料时间(s)", "出料马达速度", "搅拌马达速度", "冷热水选择")
//            val iceDesc = arrayOf("料仓编号", "出水间隔(s)", "保留", "保留", "下料量(g)", "保留", "保留", "保留")
//            val pureeDesc = arrayOf("料仓编号", "出水间隔(s)", "加水量L(ml)", "加水量H(0.1ml)", "下料时间(s)", "出料马达速度", "是否和其他BIB同时出(0,1)", "冷热水选择(0冷饮,1热饮,2冷饮加气)")
//            val milkDesc = arrayOf("料仓编号", "出水间隔(s)", "空气比例阀占空比", "蒸汽阀占空比", "下料量", "保留", "保留", "奶盒选择")
            for (index in 0 until canisterNum.toInt(16)) {
                val addIndex = 6 + index * 8
                val data0 = byteList[0 + addIndex]
                val data1 = byteList[1 + addIndex]
                val data2 = byteList[2 + addIndex]
                val data3 = byteList[3 + addIndex]
                val data4 = byteList[4 + addIndex]
                val data5 = byteList[5 + addIndex]
                val data6 = byteList[6 + addIndex]
                val data7 = byteList[7 + addIndex]
                val data0Hex = data0.toInt(16)
                val data1Hex = data1.toInt(16)
                val data2Hex = data2.toInt(16)
                val data3Hex = data3.toInt(16)
                val data4Hex = data4.toInt(16)
                val data5Hex = data5.toInt(16)
                val data6Hex = data6.toInt(16)
                val data7Hex = data7.toInt(16)
                Timber.e("$data0 $data1 $data2 $data3 $data4 $data5 $data6 $data7")
                if (CanisterIds.isCoffee(data0Hex)) {//咖啡
                    Timber.e("咖啡豆料仓:%s→%d", data0, data0Hex)
                    Timber.e("出水间隔(s):%s→%d", data1, data1Hex)
                    Timber.e("加水量(0.1ml):%s→%d", (data3 + data2), (data3 + data2).toInt(16))
                    Timber.e("磨豆时间(0.1s):%s→%d", data4, data4Hex)
                    Timber.e("去旧粉时间(s):%s→%d", data5, data5Hex)
                    Timber.e("预留:%s→%d", data6, data6Hex)
                    Timber.e("预留:%s→%d", data7, data7Hex)
                } else if (CanisterIds.isInstantAndWater(data0Hex)) {//常规料盒
                    Timber.e("料仓编号:%s→%d", data0, data0Hex)
                    Timber.e("出水间隔(s):%s→%d", data1, data1Hex)
                    Timber.e("加水量(0.1ml):%s→%d", (data3 + data2), (data3 + data2).toInt(16))
                    Timber.e("下料时间(0.1s):%s→%d", data4, data4Hex)
                    Timber.e("出料马达速度:%s→%d", data5, data5Hex)
                    Timber.e("搅拌马达速度:%s→%d", data6, data6Hex)
                    Timber.e("冷热水选择:%s→%d", data7, data7Hex)
                } else if (CanisterIds.isIce(data0Hex)) {//冰
                    Timber.e("料仓编号:%s→%d", data0, data0Hex)
                    Timber.e("出水间隔(s):%s→%d", data1, data1Hex)
                    Timber.e("保留:%s→%d", data2, data2Hex)
                    Timber.e("保留:%s→%d", data3, data3Hex)
                    Timber.e("下料量(g):%s→%d", data4, data4Hex)
                    Timber.e("保留:%s→%d", data5, data5Hex)
                    Timber.e("保留:%s→%d", data6, data6Hex)
                    Timber.e("保留:%s→%d", data7, data7Hex)
                } else if (CanisterIds.isPureeCanister(data0Hex)) {//果浆
                    Timber.e("咖啡豆料仓:%s→%d", data0, data0Hex)
                    Timber.e("出水间隔(s):%s→%d", data1, data1Hex)
                    Timber.e("加水量(0.1ml):%s→%d", (data3 + data2), (data3 + data2).toInt(16))
                    Timber.e("下料时间(0.1s):%s→%d", data4, data4Hex)
                    Timber.e("出料马达速度:%s→%d", data5, data5Hex)
                    Timber.e("是否和其他BIB同时出(0,1):%s→%d", data6, data6Hex)
                    Timber.e("冷热水选择(0冷饮,1热饮,2冷饮加气):%s→%d", data7, data7Hex)
                } else if (CanisterIds.isMilkType(data0Hex) || CanisterIds.isColdCoffee(data0Hex) || CanisterIds.isSteamCoffee(data0Hex)) {//奶
                    Timber.e("料仓编号:%s→%d", data0, data0Hex)
                    Timber.e("出水间隔(s):%s→%d", data1, data1Hex)
                    Timber.e("空气比例阀占空比:%s→%d", data2, data2Hex)
                    Timber.e("蒸汽阀占空比:%s→%d", data3, data3Hex)
                    Timber.e("下料量(s):%s→%d", data4, data4Hex)
                    Timber.e("保留:%s→%d", data5, data5Hex)
                    Timber.e("保留:%s→%d", data6, data6Hex)
                    Timber.e("奶盒选择:%s→%d", data7, data7Hex)
                } else if (data0Hex == 0x53 || data0Hex == 0x54) {
                    Timber.e("参数ID:%s", data0)
                    Timber.e("空气泵速度:%s→%d", data1, data1Hex)
                    Timber.e("泡茶时间(s):%s→%d", data2, data2Hex)
                    Timber.e("吹气时间(0.1s):%s→%d", data3, data3Hex)
                    Timber.e("浸泡比例:%s→%d", data4, data4Hex)
                    Timber.e("泡茶时间2(s):%s→%d", data5, data5Hex)
                    Timber.e("刮渣速度:%s→%d", data6, data6Hex)
                    Timber.e("保留:%s→%d", data7, data7Hex)
                } else if (data0Hex == 0x51 || data0Hex == 0x52) {
                    Timber.e("参数ID:%s", data0)
                    Timber.e("挤饼力:%s→%d", data1, data1Hex)
                    Timber.e("挤饼时间(s):%s→%d", data2, data2Hex)
                    Timber.e("回程时间(s):%s→%d", data3, data3Hex)
                    Timber.e("二次挤饼时间(s):%s→%d", data4, data4Hex)
                    Timber.e("预冲泡比例:%s→%d", data5, data5Hex)
                    Timber.e("预冲泡时间:%s→%d", data6, data6Hex)
                    Timber.e("流速控制:%s→%d", data7, data7Hex)
                } else if (data0Hex == 0x50) {
                    Timber.e("参数ID:%s", data0)
                    Timber.e("搅拌器延时停止:%s→%d", data1, data1Hex)
                    Timber.e("制作时风扇速度:%s→%d", data2, data2Hex)
                    Timber.e("产品完成延时开门(s):%s→%d", data3, data3Hex)
                    Timber.e("风扇延时停止(s):%s→%d", data4, data4Hex)
                    Timber.e("第一段混水比例:%s→%d", data5, data5Hex)
                    Timber.e("第二段混水比例:%s→%d", data6, data6Hex)
                    Timber.e("第三段混水比例:%s→%d", data7, data7Hex)
                }
                Timber.e(line)
            }

            //校验码
            val checkCode = byteList[byteList.size - 2] + byteList[byteList.size - 1]
            Timber.e("校验码:%s", checkCode)
            Timber.e(end)
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }
}
